package org.wyq.myweb.jmx.service.imple;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryManagerMXBean;
import java.lang.management.MemoryPoolMXBean;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.wyq.myweb.jmx.beaninfo.IInfoBean;
import org.wyq.myweb.jmx.beaninfo.MemoryInfoBean;
import org.wyq.myweb.jmx.service.IMXBeanService;

@Service("memorymx")
@Lazy(true)
@Primary
public class DefaultMemoryMBeanService implements IMXBeanService{
	Logger log = Logger.getLogger(this.getClass().getName());
	
	List<MemoryPoolMXBean> memPoolBeans = ManagementFactory.getMemoryPoolMXBeans();
	List<MemoryManagerMXBean> memManagerBeans = ManagementFactory.getMemoryManagerMXBeans();
	MemoryMXBean memManagerBean = ManagementFactory.getMemoryMXBean();

	@Override
	public <T extends IInfoBean> T getInfoBean() {
		MemoryInfoBean memoryInfoBean = new MemoryInfoBean();
		for (MemoryPoolMXBean mpbean : memPoolBeans) {
			String name = mpbean.getName();
			if ("PS Eden Space".equals(name))
				memoryInfoBean.setEden(mpbean.getUsage());
			else if ("PS Survivor Space".equals(name)){
				memoryInfoBean.setSurvivor(mpbean.getUsage());
//				printPool(mpbean);
			}
			else if ("PS Old Gen".equals(name))
				memoryInfoBean.setOld(mpbean.getUsage());
			else if("PS Perm Gen".equals(name))
				memoryInfoBean.setPerm(mpbean.getUsage());
			else
				log.error(name + " is not used right now");
		}
		return (T)memoryInfoBean;
	}
	
	private void printPool(MemoryPoolMXBean mpb){
		log.info("-----------------------------------");
		log.info(mpb);
		log.info(mpb.getName());
		log.info(JSONObject.fromObject(mpb.getCollectionUsage()));
		log.info(JSONArray.fromObject(mpb.getMemoryManagerNames()));
		log.info(JSONObject.fromObject(mpb.getObjectName()));
		log.info(JSONObject.fromObject(mpb.getPeakUsage()));
		log.info(JSONArray.fromObject(mpb.getType()));
		log.info(JSONObject.fromObject(mpb.getUsage()));
		log.info("\n");
	}
	
	public static void main(String[] args){
		DefaultMemoryMBeanService s = new DefaultMemoryMBeanService();
		System.out.println("Start reading memory pool mx bean[" + s.memPoolBeans.size() + "]");
		for(MemoryPoolMXBean mpbean: s.memPoolBeans){
			System.out.println(JSONObject.fromObject(mpbean.getCollectionUsage()));
//			System.out.println(JSONObject.fromObject(mpbean.getCollectionUsageThreshold()));
//			System.out.println(JSONObject.fromObject(mpbean.getCollectionUsageThresholdCount()));
			System.out.println(JSONArray.fromObject(mpbean.getMemoryManagerNames()));
			System.out.println(mpbean.getName());
			System.out.println(JSONObject.fromObject(mpbean.getObjectName()));
			System.out.println(JSONObject.fromObject(mpbean.getPeakUsage()));
			System.out.println(JSONArray.fromObject(mpbean.getType()));
			System.out.println(JSONObject.fromObject(mpbean.getUsage()));
//			System.out.println(JSONObject.fromObject(mpbean.getUsageThreshold()));
//			System.out.println(JSONObject.fromObject(mpbean.getUsageThresholdCount()));
		}
		System.out.println("End reading memory pool mx bean");
		System.out.println();
		System.out.println("Start reading memory manager mx bean[" + s.memManagerBeans.size() + "]");
		for(MemoryManagerMXBean mmbean: s.memManagerBeans){
			System.out.println(JSONArray.fromObject(mmbean.getMemoryPoolNames()));
			System.out.println(mmbean.getName());
			System.out.println(JSONObject.fromObject(mmbean.getObjectName()));
		}
		System.out.println("End reading memory manager mx bean");
		System.out.println();
		System.out.println("Start reading a memory mx bean");
		System.out.println(JSONObject.fromObject(s.memManagerBean.getHeapMemoryUsage()));
		System.out.println(JSONObject.fromObject(s.memManagerBean.getNonHeapMemoryUsage()));
		System.out.println(JSONObject.fromObject(s.memManagerBean.getObjectName()));
		System.out.println(JSONObject.fromObject(s.memManagerBean.getObjectPendingFinalizationCount()));
		System.out.println("End reading a memory mx bean");
	}

}




